import random
import time

import win32api
import win32con
import win32gui


class MouseControl:
    # 句柄
    hwnd_ = -1

    def __init__(self, hwnd):
        self.hwnd_ = hwnd

    def move(self, targetXY):
        win32api.SendMessage(self.hwnd_, win32con.WM_MOUSEMOVE, win32con.MK_LBUTTON, targetXY)

    def click(self, target_x, target_y):
        """
        在窗口内进行左键单击
        :param target_x: x坐标
        :param target_y: y坐标
        """
        # 模拟鼠标指针，指定坐标
        tempPosition = win32api.MAKELONG(target_x, target_y)
        # 移动到指定位置
        self.move(tempPosition)
        # 模拟鼠标按下
        win32api.SendMessage(self.hwnd_, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tempPosition)
        # time.sleep(0.01)
        # 模拟鼠标弹起
        win32api.SendMessage(self.hwnd_, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tempPosition)

    def rightClick(self, target_x, target_y):
        """
        在窗口内进行右键单击
        :param target_x: x坐标
        :param target_y: y坐标
        """
        # 模拟鼠标指针，指定坐标
        tempPosition = win32api.MAKELONG(target_x, target_y)
        # 移动到指定位置
        self.move(tempPosition)
        # 模拟鼠标按下
        win32api.SendMessage(self.hwnd_, win32con.WM_RBUTTONDOWN, win32con.MK_LBUTTON, tempPosition)
        # time.sleep(0.05)
        # 模拟鼠标弹起
        win32api.SendMessage(self.hwnd_, win32con.WM_RBUTTONUP, win32con.MK_LBUTTON, tempPosition)

    def myPrint(self):
        print(self.lastXY)


def main():
    # 获取窗口句柄
    hwnd = win32gui.FindWindow(R"SDL_app", R"M2007J17C")
    # hwnd = win32gui.FindWindow(R"WeChatMainWndForPC", R"微信")
    rect = win32gui.GetWindowRect(hwnd)
    print(rect)

    # 设置目标位置（相对于窗口）
    target_x = 200
    target_y = 130


    # 设置移动持续时间（秒）
    mouse = MouseControl(hwnd)
    mouse.click(target_x, target_y)
    # mouse.moveMouse(hwnd, target_x, target_y)

    # 模拟鼠标指针，指定坐标
    # long_position = win32api.MAKELONG(target_x, target_y)
    # 模拟鼠标按下
    # win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position)
    # 模拟鼠标弹起
    # win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position)


if __name__ == "__main__":
    main()
